/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, {Component} from "react";
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Dimensions,
    TouchableHighlight,
    Navigator,
    Image,
    ListView
} from "react-native";
import TestDBManager from "./app/TestDBManager";
import TestShareManager from "./app/TestShareManager";
import TestRnImageUtil from "./app/TestRnImageUtil";
import TestNetWorkManager from "./app/TestNetWorkManager";
import TestTextToSpeechManager from "./app/TestTextToSpeechManager";
import TestProtoManager from "./app/TestProtoManager";

//var messages = require('./Babybath_pb');


// import proto from './Babybath.proto';


export default class MyNpm extends Component {
    static propTypes = {
        navigator: React.PropTypes.object.isRequired,
    };
    //
    constructor(props) {
        super(props);

        this.state = {
            winWidth: 0,
            winHeight: 0,

        };
        // 在ES6中，如果在自定义的函数里使用了this关键字，则需要对其进行“绑定”操作，否则this的指向不对
        // 像下面这行代码一样，在constructor中使用bind是其中一种做法（还有一些其他做法，如使用箭头函数等）


    }

    //
    componentWillMount() {
        var win_info = Dimensions.get('window');
        this.setState({
            winWidth: win_info.width,
            winHeight: win_info.height,
        });
        this.props.pro
    }


    test_DBManager() {
        const {navigator} = this.props;

        if (navigator) {
            navigator.push({
                name: "TestDBManager",
                component: TestDBManager,
            });
        } else {
            alert(navigator);
        }
    }

    test_ShareManager() {
        const {navigator} = this.props;

        if (navigator) {
            navigator.push({
                name: "TestShareManager",
                component: TestShareManager,
            });
        } else {
            alert(navigator);
        }
    }

    test_RNImageUtil() {
        const {navigator} = this.props;

        if (navigator) {
            navigator.push({
                name: "TestRnImageUtil",
                component: TestRnImageUtil,
            });
        } else {
            alert(navigator);
        }
    }

    test_NetWorkManager() {
        const {navigator} = this.props;

        if (navigator) {
            navigator.push({
                name: "TestNetWorkManager",
                component: TestNetWorkManager,
            });
        } else {
            alert(navigator);
        }
    }

    test_TestTextToSpeechManager() {
        const {navigator} = this.props;

        if (navigator) {
            navigator.push({
                name: "TestTextToSpeechManager",
                component: TestTextToSpeechManager,
            });
        } else {
            alert(navigator);
        }
    }

    test_TestProtoManager() {
        const {navigator} = this.props;

        if (navigator) {
            navigator.push({
                name: "TestProtoManager",
                component: TestProtoManager,
            });
        } else {
            alert(navigator);
        }
    }

    // test_Proto3() {
    //
    //     var message = new messages2.BabyBathMessage();
    //     // message.setLoopcount(10);
    //     message.setWeight(3)
    //     console.log("serializeBinary", message.serializeBinary());
    //     console.log("deserializeBinary", messages2.BabyBathMessage.deserializeBinary(message.serializeBinary()).array)
    // }


    render() {

        return (
            <View style={styles.container}>
                <Text onPress={this.test_DBManager.bind(this)}
                      style={styles.testSize}>
                    DBManager
                </Text>
                <Text onPress={this.test_ShareManager.bind(this)}
                      style={styles.testSize}>
                    ShareManager
                </Text>
                <Text onPress={this.test_RNImageUtil.bind(this)}
                      style={styles.testSize}>
                    RNImageUtil
                </Text>
                <Text onPress={this.test_NetWorkManager.bind(this)}
                      style={styles.testSize}>
                    NetWorkManager
                </Text>
                <Text onPress={this.test_TestTextToSpeechManager.bind(this)}
                      style={styles.testSize}>
                    TextToSpeechManager
                </Text>
                <Text onPress={this.test_TestProtoManager.bind(this)}
                      style={styles.testSize}>
                    TextProtoManager
                </Text>


            </View>
        )
    }
}

var styles = StyleSheet.create({
        container: {
            flex: 1,
            padding: 20,

            justifyContent: 'flex-start',
            alignItems: 'center',
            flexWrap: 'wrap',
            backgroundColor: '#F5FCFF',

        },
        testSize: {
            margin: 5,
            backgroundColor: '#dddddd',
            fontSize: 18,
            padding: 20,
        }

    }
);
/**
 * 构建一个component 用来当程序入口
 * 注意，需要在程序入口保存适当内容，用于后续比如跳转界面等，如以下的navigator是跳转必须的
 */
class Start extends Component {
    render() {
        var defaultName = 'MyNpm';
        var defaultComponent = MyNpm;
        return (
            <Navigator
                //指定了默认的页面，也就是启动app之后会看到的第一屏，需要两个参数，name跟component
                initialRoute={{ name: defaultName, component: defaultComponent }}
                configureScene={
                    (route) => {
                                      //跳转的动画
                                      return Navigator.SceneConfigs.FloatFromLeft;
                    }
                }
                renderScene={
                    (route, navigator) => {
                                      let Component = route.component;
                                      if(route.component){
                                              return <Component {...route.params} navigator={navigator} />
                                      }
                    }
                }
            />
        );
    }
}


AppRegistry.registerComponent('MyNpm', () => Start);



